package com.example.javaee_library.Controller;
/**
 * @author Miao-yongzheng
 * @create 2021-12-14 21:17
 */

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.example.javaee_library.Entity.User;
import com.example.javaee_library.Service.Impl.MailService;
import com.example.javaee_library.Service.User_Service;
import com.example.javaee_library.dao.UserDao;
import com.example.javaee_library.dto.JsonData;
import com.example.javaee_library.exception.NoUserException;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.List;

/**
 * @description:
 * @author: 34911
 * @time: 2021/12/14 21:17
 */
@Lazy
@Slf4j
@Controller
@RequestMapping("/Customer/Admin_User")
public class Admin_UserController {
    @Autowired
    UserDao userDao;
    @Autowired
    User_Service user_service;
    @Autowired
    MailService mailService;
    @ResponseBody
    @GetMapping("/delete")
    private JsonData delete(@RequestParam("UserId") String UserId){
        log.warn(DateUtil.date(Calendar.getInstance()).toString()+"删除用户"+UserId);
        int back = userDao.Delete(Integer.parseInt(UserId));
        // list.jsp + model = ModelAndView
        if(back==0)
        {
            throw new NoUserException(611,"无此用户");
        }
        return JsonData.buildSuccess("删除成功");
    }

    @GetMapping("/Select")
    public ModelAndView Select(ModelAndView modelAndView)
    {
        log.info( DateUtil.date(Calendar.getInstance()).toString()+"查询了用户");
        List<User> users = userDao.selectAll();
        modelAndView.addObject("list",users);
        modelAndView.setViewName("listUser");
        return modelAndView;
    }


    @PostMapping("/UpdateByUser")
    public JsonData UpdateByUser(@RequestParam("UserId") String oldUserId,
                           @RequestParam("Password") String Password
                           )
    {
        if(userDao.Serach_Byuserid(oldUserId)==null){
            return JsonData.buildError("修改失败,没有此ID");
        }
      if(user_service.UpdateByUser(Integer.parseInt(oldUserId),Password))
      {
          return JsonData.buildSuccess("修改成功");
      }
      else
          return JsonData.buildError("修改失败");
    }

    @PostMapping("/UpdateByAdmin")
    public JsonData UpdateByAdmin(
        @RequestBody User user
    )
    {
        if(userDao.Serach_Byuserid(String.valueOf(user.getId()))==null){
            return JsonData.buildError("修改失败,没有此ID");
        }
       else if(user_service.UpdateByAdmin(user.getId(),user.getPassword(),user.getAuthority(),user.getState(),user.getDeleted()))
        {
            return JsonData.buildSuccess("修改成功");
        }
        else
            return JsonData.buildError("修改失败");
    }

    @RequestMapping(value = "/listuser", method = RequestMethod.GET)
    public ModelAndView listuser(ModelAndView model,@RequestParam("UserId") String UserId,
                                 HttpServletResponse response,
                                 HttpServletRequest request) {
        System.out.println("调用了listuser");
        List<User> list = user_service.Serach(UserId);

        request.getSession().setAttribute("list",list);
        model.addObject("list", list);
        model.setViewName("listUser");
        // list.jsp + model = ModelAndView
        return model;// WEB-INF/jsp/"list".jsp
    }


    @ResponseBody
    @RequestMapping(value = "/hint", method = RequestMethod.GET)
    public JsonData hint(       @RequestParam("student_id")String student_id,
                                 HttpServletResponse response,
                                 HttpServletRequest request) {
        User user = userDao.Serach_Byuserid(student_id);//精准查询
        String email = user.getEmail();
        mailService.sendSimpleMail(email,"永正图书馆系统提醒",
                "您有书即将预期,请及时归还"
        );

        // list.jsp + model = ModelAndView
        return JsonData.buildSuccess("发送成功");// WEB-INF/jsp/"list".jsp
    }

    @ResponseBody
    @RequestMapping(value = "/freeze", method = RequestMethod.GET)
    public JsonData freeze(       @RequestParam("student_id")String student_id,
                                  @RequestParam("freeze") String freeze
                                ) {
        if(freeze.equals("冻结"))
            user_service.freeze(Integer.parseInt(student_id));
        else if(freeze.equals("解冻"))
            user_service.unfreeze(Integer.parseInt(student_id));
        // list.jsp + model = ModelAndView
        return JsonData.buildSuccess("冻结成功");// WEB-INF/jsp/"list".jsp
    }
}
